package com.daoeping.dao;

import com.daoeping.data.Usr_user;
import com.daoeping.domain.JsonData;
import com.daoeping.domain.Page;
import com.daoeping.domain.TableData;
import com.daoeping.util.ConverntJsonData;
import com.daoeping.util.Md5Password;
import com.opensymphony.xwork2.ActionContext;

import java.util.List;

import org.json.JSONArray;
import org.springframework.transaction.annotation.Transactional;



public class Usr_userDAO extends AllDaoSupport {
	
	
	
	public boolean findUsr_userByNameAndPass(Usr_user usr_user){
		String hql = "from Usr_user user  where user.username = '"+usr_user.getUsername()+"' and user.password='"+new Md5Password().setmd5Pass(usr_user.getPassword())+"'";
		List<Usr_user> usr_userList = (List<Usr_user>)selectDataByHQL(hql);
		if(usr_userList.size()==1){
			ActionContext.getContext().getSession().put("user", usr_userList.get(0));
			return true;
		}else{
			return false;
		}
	}
	
	public boolean findUsr_userByName(Usr_user usr_user){
		String hql = "from Usr_user user  where user.username = '"+usr_user.getUsername()+"'";
		List<Usr_user> usr_userList = (List<Usr_user>)selectDataByHQL(hql);
		if(usr_userList.size()==1){
			return true;
		}else{
			return false;
		}
	}
	
	public JsonData findAllUsr_userNotFriendByPage(Page page, TableData tableData ,Usr_user usr_user){
		try {
			StringBuilder sb1 = new StringBuilder();
			StringBuilder sb2 = new StringBuilder();
			sb1.append(" select user.id ID, usrbasic.nickname NICKNAME, usrbasic.gender GENDER, usrbasic.birthday BIRTHDAY from  usr_user user "+
			" left join usr_userbasicinfo usrbasic on user.id = usrbasic.user_id "+
			" where user.id not in (select fri.friend_id from usr_friend fri where user_id = "+usr_user.getId()+" and friend_id is not null ) and user.id !=  "+usr_user.getId() + 
			" and usrbasic.nickname  like '%"+usr_user.getUsername()+"%'");
			sb2.append(" select count(user.id) from  usr_user user "+
			" left join usr_userbasicinfo usrbasic on user.id = usrbasic.user_id "+
			" where user.id not in (select fri.friend_id from usr_friend fri where user_id = "+usr_user.getId()+" and friend_id is not null ) and user.id !=  "+usr_user.getId() + 
			" and usrbasic.nickname  like '%"+usr_user.getUsername()+"%'");
			sb1.append(" order by user.id desc");
			List<Object[]> resultlist = (List<Object[]>) this.getPageList(page, sb1.toString());	
			JSONArray json = new ConverntJsonData().getJsonData(resultlist);
			JSONArray newJson = new JSONArray();
			newJson.put(this.getNum(page, sb2.toString()));
			//friend group detail
			Object []obj = new Object[3];
			newJson.put(obj);
			for (int i = 0; i < json.length(); i++) {
				newJson.put(json.get(i));
			}
			JsonData jsonData = new JsonData();
			jsonData.setJsonData(newJson.toString());
			System.out.println(newJson);
			return jsonData;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	@Override
	@Transactional
	public Object getPageList(Page page, String sql) {
		int temp = (page.getPage() - 1) * page.getPageSize();
		return pageUsr_userBySQL(sql , temp ,page.getPageSize() );
		
	}
	@Transactional
	public Object pageUsr_userBySQL(String sql, int offSet, int pageSize) {
		System.out.println("operation:select,hql:"+sql);
		log.debug("operation:select,hql:"+sql+",offSet:"+offSet+",pageSize:"+pageSize);
		try {
			return sessionFactory.getCurrentSession().createSQLQuery(sql).addScalar("ID").
					addScalar("NICKNAME").addScalar("GENDER").addScalar("BIRTHDAY").
					setFirstResult(offSet).setMaxResults(pageSize).list();
		} catch (Exception e) {
			return false;
		}
	}
	@Override
	@Transactional
	public int getNum(Page page, String hql)throws Exception{
		
		List<?> list = (List<?>) selectDataBySQL(hql);
		System.out.println(list+"count:");
		int rowNum = 0;
		if (list.size() != 0) {
			rowNum = Integer.parseInt(list.get(0).toString());
		}
		return rowNum;
	}
}